package ipdesk.model.businessInfo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.opensymphony.xwork2.ActionContext;

import ecg.util.SessionUtils;



import ipdesk.model.businessInfo.bean.BusinessCustomerInfoBean;
import ipdesk.model.businessInfo.bean.BusinessInfoBean;
import ipdesk.model.report.promote.bean.PromoteInfoBean;

public class BusinessInfoImpl extends SqlMapDaoTemplate implements IBusinessInfo
{
	
	public BusinessInfoImpl(DaoManager manager)
	{
		super(manager);
	}
	
	public int getCompanyCodeCount(BusinessInfoBean businessInfoBean)
	{	
		// 기업코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		int companyCodeCount = ((Integer)this.queryForObject("biz.companyCode-count", businessInfoBean)).intValue();
		
		return companyCodeCount;
	}
	
	/**
	 * 사용자에게서 입력받은 기업정보 데이터를 DB에 insert 하는 메서드.
	 */
	public int insertBusinessInfo(BusinessInfoBean businessInfoBean)
	{
		int lastCompanySeq = ((Integer)this.insert("biz.bizInfoInsert", businessInfoBean)).intValue();
		
		return lastCompanySeq;
	}
	
	
	public int businessInfoReg(BusinessInfoBean businessInfoBean) throws Exception
	{

		int companyCodeCount = 0;
		int lastCompanySeq = 0;
		String companyCode = "";
		
		
		String regTime = businessInfoBean.getRegTime();
		String deskCode = businessInfoBean.getDeskCode();
		
		
		
		// 1. 기업코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		companyCodeCount = ((Integer)this.queryForObject("biz.companyCode-count", businessInfoBean)).intValue();
		
		
				
		String endNumber = StringUtils.leftPad(Integer.toString(companyCodeCount+1), 5, "0");
		
		
		// 생성된 기업관리코드.
		companyCode = regTime.replaceAll("-","") + "-" + deskCode.substring(4) + "-C" + endNumber;
		
				
		// 생성된 기업관리코드 빈 객체에 저장.
		businessInfoBean.setCompanyCode(companyCode);
		businessInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// 2. iBatis 연동. 데이터 insert. 마지막으로 insert 된 기업정보의 PK를 반환받는다.
		lastCompanySeq = ((Integer)this.insert("biz.bizInfoInsert", businessInfoBean)).intValue();
			
		
		return lastCompanySeq;
	}
	
	
	
	/**
	 * 상담의뢰 담당자 정보를 DB에 insert 하는 메서드.
	 */
	public void bizCustomerInfoReg(BusinessInfoBean businessInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("biz.customerInfoInsert", businessInfoBean);
	}
	
	public int getDataTotalCount(BusinessInfoBean businessInfoBean) throws Exception
	{
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		if(businessInfoBean.getDeskCode() == null || "".equals(businessInfoBean.getDeskCode())){
			map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		}else{
			map.put("deskCode", businessInfoBean.getDeskCode());
		}
		
		if(businessInfoBean.getSearchKeyword() != null || !"".equals(businessInfoBean.getSearchKeyword())){
			map.put("searchKeyword", businessInfoBean.getSearchKeyword());
			map.put("searchWhere", businessInfoBean.getSearchWhere());
		}
		
		int totalDataCount = ((Integer)this.queryForObject("biz.bizInfoTotalcount", map)).intValue();
		
		return totalDataCount;
	}
	
	public List<BusinessInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		System.out.println("관리자 코드: " + map.get("authCode"));
		System.out.println("데스크 코드: " + map.get("deskCodeSelected"));
		List<BusinessInfoBean> pageDataList = this.queryForList("biz.bizPageDataListForUser", map);
		
		return pageDataList;
	}
	
	public List<BusinessCustomerInfoBean> getAllCustomerListForCompany(int companySeq)
	{
		List<BusinessCustomerInfoBean> list = this.queryForList("biz.select-all-customer-for-company", companySeq);
		
		return list;
	}
	
	public List getCompanyList(String deskCode)
	{
		List list = this.queryForList("biz.bizInfoLocalNameSelect", deskCode);
		
		return list;
	}
	
	public List getCustomerList(int companySeq)
	{
		List list = this.queryForList("biz.bizInfoCustomerSelect", companySeq);
		
		return list;
	}
	
	/**
	 *  해당 companySeq 또는 customerSeq의 하나의 로우 데이터를 가지고온다.
	 *  
	 */
	public BusinessInfoBean getDataOneRow(int idx, String queryId)
	{
		BusinessInfoBean businessInfoBean = (BusinessInfoBean)this.queryForObject(queryId, idx);
		
		return businessInfoBean;
	}
	
	/**
	 *  해당 companySeq의 기업정보를 업데이트한다.
	 */
	public int companyUpdate(BusinessInfoBean businessInfoBean, String queryId)
	{
		int result = this.update(queryId, businessInfoBean);
		
		return result;
	}
	
	/**
	 * 해당 기업정보 삭제
	 */
	public void bizCompanyInfoDelete(BusinessInfoBean businessInfoBean, String queryId)
	{
		this.delete(queryId, businessInfoBean);
	}
	
	/**
	 * 기업 담당자 정보 업데이트
	 */
	public void bizCustomerInfoUpdate(BusinessInfoBean businessInfoBean, String queryId)
	{
		this.update(queryId, businessInfoBean);
	}
	
	/**
	 * 기업 담당자 데이터 삭제
	 */
	public void bizCustomerInfoDelete(BusinessInfoBean businessInfoBean, String queryId)
	{
		this.delete(queryId, businessInfoBean);
	}

	/**
	 * 회사명 검색
	 */
	public List<BusinessInfoBean> getSearchCompany(BusinessInfoBean businessInfoBean) {
		// TODO Auto-generated method stub
		return this.queryForList("biz.select-search-company", businessInfoBean);
	}

	/**
	 * 회사명 검색 수
	 */
	public int getSearchCount(BusinessInfoBean businessInfoBean) {
		// TODO Auto-generated method stub
		return (Integer)this.queryForObject("biz.select-search-count", businessInfoBean);
	}

	/**
	 * 현지 업체명 중복 확인 
	 */
	public int checkHeadName(BusinessInfoBean businessInfoBean) {
		// TODO Auto-generated method stub
		return (Integer)this.queryForObject("biz.checkHeadName", businessInfoBean);
	}
}
